package module.edittroop.view.troopinfopanel
{
	import code.utils.MaterialUtil;
	
	import com.darwin.darwinUI.containers.ContainerBase;
	import com.darwin.darwinUI.controls.Button;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.controls.TextInput;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.general.TextFieldFactory;
	import com.darwin.darwinUI.manager.UIManager;
	
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.media.Video;
	import flash.text.TextFieldType;
	
	import module.edittroop.event.EditTroopEvent;
	import module.edittroop.view.editnewtrooppanel.ChoosePanel;
	import module.edittroop.view.editnewtrooppanel.EditNewTroopPanel;
	import module.main.view.ResourceConst;

	public class EditTroopPanel extends ContainerBase
	{
		private var _editTroopBg:Sprite;
		
		private var _textInSp:Sprite;
		private var _textInSpTxt:Label;
		
		private var _closeBtn:Button;
		
		private var _uiconpd:UIComponent;
		
		private var _troopInfoPanel:TroopInfoPanel;
		/**
		 *重组按钮 
		 */		
		private var _reorganizationBtn:Button;
		/**
		 *取消按钮 
		 */		
		private var _chanelBtn:Button;
		
		public var _heroAvatarPanel:HeroAvatarPanel;
		public function EditTroopPanel()
		{
			_uiconpd = new UIComponent();
			_troopInfoPanel = new TroopInfoPanel();
			_heroAvatarPanel = new HeroAvatarPanel();
			this.addItem(_uiconpd);
			initView();
			this.height = _editTroopBg.height + _textInSp.height / 2 ;
			this.width = _editTroopBg.width;
			_uiconpd.y = 20
		}
		public function get troopInfoPanel():TroopInfoPanel
		{
			return _troopInfoPanel;
		}
		private function initView():void
		{
			_reorganizationBtn = new Button();
			_reorganizationBtn.backgroundSkin = "EditTroop." + ResourceConst.EDIT_BLUE_BUTTON;
			
			_chanelBtn = new Button();
			_chanelBtn.backgroundSkin = "EditTroop." + ResourceConst.EDIT_TROOP_DISSOLVE_BUTTON;
			
			_closeBtn = new Button();
			_closeBtn.backgroundSkin = "EditTroop." + ResourceConst.EDIT_TROOP_COLSE_BUTTON;
			_closeBtn.backgroundAuto = true;
			
			_textInSpTxt = new Label();
			_textInSpTxt = TextFieldFactory.getInstance().getLabel("create_title_1");
			_textInSpTxt.text = "编辑部队";
			//_textInSpTxt = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_ICON) as Sprite;
			
			_editTroopBg = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_BACKGROUND) as Sprite;
			_textInSp = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_TEXTINPUT_PANEL) as Sprite;
			setUiPosition();
		}
		/**
		 *设置UI的位置 
		 * 
		 */		
		private function setUiPosition():void
		{
			_uiconpd.addChild(_editTroopBg);
			//输入框背景UI
			setTextInSp();
			setTroopInfoPanel();
			setReorganizationBtn();
			//取消按钮
			setChanelBtn();
			//关闭按钮
			setCloseBtn();
			setHeroAvatarPanel();
		}
		private function setCloseBtn():void
		{
			_closeBtn.x = _editTroopBg.x + _editTroopBg.width - 42;
			_closeBtn.y = 13;
			_uiconpd.addChild(_closeBtn);
			_closeBtn.addEventListener(MouseEvent.CLICK,mouseClickHandler);
		}
		private function setTextInSp():void
		{
			_textInSp.x = _editTroopBg.x + (_editTroopBg.width - _textInSp.width) / 2;
			_textInSp.y = _editTroopBg.y - _textInSp.height / 2 + 5;
			_textInSpTxt.x = (_textInSp.width - _textInSpTxt.width) / 2;
			_textInSpTxt.y =  (_textInSp.height - _textInSpTxt.height) / 2 - 5;
			_editTroopBg.addChild(_textInSp);
			_textInSp.addChild(_textInSpTxt);
		}
		private function setTroopInfoPanel():void
		{
			_troopInfoPanel.x = _editTroopBg.x + (_editTroopBg.width - _troopInfoPanel.width) / 2;
			_troopInfoPanel.y = _editTroopBg.y  + (_editTroopBg.height - _troopInfoPanel.height) / 4 * 3 - 10;
			addItem(_troopInfoPanel);
		}
		private function setReorganizationBtn():void
		{
			var $label:Label = new Label;
			$label = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			$label.text = "重组";
			_reorganizationBtn.width = 82.0;
			_reorganizationBtn.height = 34.0;
			_reorganizationBtn.x = _editTroopBg.x + _editTroopBg.width / 2 - _reorganizationBtn.width - 15;
			_reorganizationBtn.y = (_editTroopBg.height - _troopInfoPanel.height -_troopInfoPanel.y ) / 2 + _troopInfoPanel.height +_troopInfoPanel.y - 40;
			$label.x = (_reorganizationBtn.width - $label.width) / 2;
			$label.y = (_reorganizationBtn.height - $label.height) / 2;
			_reorganizationBtn.addEventListener(MouseEvent.CLICK,mouseClickHandler);
			_uiconpd.addChild(_reorganizationBtn);
			_reorganizationBtn.addChild($label);
		}
		private function setChanelBtn():void
		{
			_chanelBtn.width = 82.0;
			_chanelBtn.height = 34.0;
			_chanelBtn.x = _editTroopBg.x + _editTroopBg.width / 2 + 15;
			_chanelBtn.y = _reorganizationBtn.y;
			_uiconpd.addChild(_chanelBtn);
			_chanelBtn.addEventListener(MouseEvent.CLICK,mouseClickHandler);
		}
		
		private function mouseClickHandler(e:MouseEvent):void
		{
			if(e.currentTarget ==_chanelBtn )
			{
				var choosePanelEvent:EditTroopEvent = new EditTroopEvent(EditTroopEvent.GET_CHANGLE_PANEL)
					this.dispatchEvent(choosePanelEvent);

			}
			if(e.currentTarget ==_reorganizationBtn)
			{
				var getNewTroopPanel:EditTroopEvent = new EditTroopEvent(EditTroopEvent.GET_DISSLOVE_TROOP_PANEL);
				dispatchEvent(getNewTroopPanel);
			}
			if(e.currentTarget == _closeBtn)
			{
				var removeEvent:EditTroopEvent = new EditTroopEvent(EditTroopEvent.REMOVE_TROOP_EVENT);
				dispatchEvent(removeEvent);
			}
		}
		private function setHeroAvatarPanel():void
		{
			_heroAvatarPanel.x = _troopInfoPanel.x;
			_heroAvatarPanel.y = 45;
			_uiconpd.addChild(_heroAvatarPanel);
		}
		
	}
}